package com.itheima.homework;

public class Test3 {
    //3.定义一个方法，该方法可以求1~n的和  。n的值是调用时传入
    public static void main(String[] args) {
        int sum = getSum(0);
        if(sum!=-1){
            System.out.println(sum);
        }

    }

    public static int getSum(int n) {
        //过滤
        if(n<1){
            System.out.println("传入的n非法");
            return -1;
        }

        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += i;
        }
        return sum;
    }
}
